﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt.UserControls
{
    public partial class joinbox : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                string username = HttpContext.Current.User.Identity.Name;
                DTO.Member member = MemberProcessorInstance.GetMemberByUsername(username);
                int CurrentUserId = member.Id;

                // if users is not a member, show join button
                // if user is a member, dont show join button, show leave button
                // if user has joined but not approved, show 'approval in process' and no join button


                //check if current user is a member of the book club and the club is not secret
                if (BookClubProcessorInstance.IsBookClubMember(CurrentUserId, CID))
                {
                    btnJoin.Visible = false;
                    btnLeaveClub.Visible = true;
                    lblMemberOrNot.Visible = true;

                }
                    // check if they are in pending status
                else if (BookClubProcessorInstance.IsPendingBookClubMember(CurrentUserId, CID))
                {
                    btnJoin.Visible = false;
                    btnLeaveClub.Visible = false;
                    lblMemberOrNot.Visible = true;
                    lblMemberOrNot.Text = "Your member status is pending";

                }
                // if the member is a leader they cant leave
                if (BookClubProcessorInstance.IsLeader(CurrentUserId, CID))
                {
                    btnJoin.Visible = false;
                    btnLeaveClub.Visible = false;
                    lblMemberOrNot.Visible = true;
                    lblMemberOrNot.Text = "You are a leader of this club.";

                }
            }
            // if is not logged in
            else
            {
                btnJoin.Visible = false;
                btnLeaveClub.Visible = false;
                lblMemberOrNot.Visible = true;
                lblMemberOrNot.Text = "Please <a href='../register.aspx'>register</a> to join this club.";
            }
        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            DTO.BookClub club = BookClubProcessorInstance.GetBookClubById(CID);
            DTO.Member admin = MemberProcessorInstance.GetBookClubAdminById(CID);

            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {

                // 1 = public, 2 = Private, 3 = secret

                // if public club, join member to club
                if(club.ClubType == 1 && System.Web.HttpContext.Current.User.Identity.IsAuthenticated){
                    BookClubProcessorInstance.JoinBookClub(CID, CurrentUserId,true);
                    BookClubProcessorInstance.AddClubActivity(CID,CurrentUserId,"JOINED",CID);
                    EmailProcessorInstance.SomeoneJoinedClubEmail(CID, admin.Id, false);
                    BindData();
                }
                else if (club.ClubType == 2 && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    BookClubProcessorInstance.JoinBookClub(CID, CurrentUserId, false);
                    lblMemberOrNot.Text = "Awaiting approval from leaders.";
                    EmailProcessorInstance.SomeoneJoinedClubEmail(CID, admin.Id, true);
                    BindData();

                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }


        }

        protected void btnLeaveClub_Click(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {

                // if logged in and is member and is NOT leader
                if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    BookClubProcessorInstance.LeaveBookClub(CID, CurrentUserId);
                    BookClubProcessorInstance.AddClubActivity(CID, CurrentUserId, "UNJOINED", CID);
                    Page.Response.Redirect(Page.Request.Url.ToString(), true);
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }

        }
    }
}